package com.chatai.service;

import com.chatai.model.ApiKey;
import com.chatai.model.User;
import com.chatai.repository.ApiKeyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ApiKeyService {
    
    @Autowired
    private ApiKeyRepository apiKeyRepository;
    
    /**
     * 创建新的API Key
     */
    public ApiKey createApiKey(User user, String description, LocalDateTime expiresAt) {
        ApiKey apiKey = new ApiKey();
        apiKey.setUser(user);
        apiKey.setDescription(description);
        apiKey.setExpiresAt(expiresAt);
        apiKey.setActive(true);
        return apiKeyRepository.save(apiKey);
    }
    
    /**
     * 验证API Key是否有效
     */
    public boolean isValidApiKey(String keyValue) {
        Optional<ApiKey> apiKeyOpt = apiKeyRepository.findByKeyValue(keyValue);
        if (apiKeyOpt.isEmpty()) {
            return false;
        }
        
        ApiKey apiKey = apiKeyOpt.get();
        // 检查是否激活
        if (!apiKey.isActive()) {
            return false;
        }
        
        // 检查是否过期
        if (apiKey.getExpiresAt() != null && apiKey.getExpiresAt().isBefore(LocalDateTime.now())) {
            return false;
        }
        
        return true;
    }
    
    /**
     * 获取API Key对应的用户
     */
    public Optional<User> getUserByApiKey(String keyValue) {
        Optional<ApiKey> apiKeyOpt = apiKeyRepository.findByKeyValue(keyValue);
        if (apiKeyOpt.isPresent() && isValidApiKey(keyValue)) {
            return Optional.of(apiKeyOpt.get().getUser());
        }
        return Optional.empty();
    }
    
    /**
     * 获取用户的所有API Key
     */
    public List<ApiKey> getUserApiKeys(Long userId) {
        return apiKeyRepository.findByUserId(userId);
    }
    
    /**
     * 删除API Key
     */
    public void deleteApiKey(Long keyId, Long userId) {
        Optional<ApiKey> apiKeyOpt = apiKeyRepository.findById(keyId);
        if (apiKeyOpt.isPresent() && apiKeyOpt.get().getUser().getId().equals(userId)) {
            apiKeyRepository.deleteById(keyId);
        }
    }
    
    /**
     * 禁用API Key
     */
    public void deactivateApiKey(Long keyId, Long userId) {
        Optional<ApiKey> apiKeyOpt = apiKeyRepository.findById(keyId);
        if (apiKeyOpt.isPresent() && apiKeyOpt.get().getUser().getId().equals(userId)) {
            ApiKey apiKey = apiKeyOpt.get();
            apiKey.setActive(false);
            apiKeyRepository.save(apiKey);
        }
    }
    
    /**
     * 启用API Key
     */
    public void activateApiKey(Long keyId, Long userId) {
        Optional<ApiKey> apiKeyOpt = apiKeyRepository.findById(keyId);
        if (apiKeyOpt.isPresent() && apiKeyOpt.get().getUser().getId().equals(userId)) {
            ApiKey apiKey = apiKeyOpt.get();
            apiKey.setActive(true);
            apiKeyRepository.save(apiKey);
        }
    }
}